
add() {
        local name=$1
        shift
        local ips=$@
        local i=1
for ip in $ips; do
        local targetname=$name-$ip
        local targetimg=$workdir/$targetname.img
        virsh list --all | grep -q $targetname && continue
        [ -f $targetimg ] || cp -a $template $targetimg

        if echo $template | grep rocky -q ; then
                virt-sysprep -a $targetimg --root-password password:${root_passwd} --run-command "echo -e '[connection]\nid=enp1s0\ntype=ethernet\ninterface-name=enp1s0\n[ethernet]\n[ipv4]\nmethod=manual\naddress1=$ip/24,192.168.0.1\ndns=114.114.114.114;\n[ipv6]\nmethod=disabled\n[proxy]\n' > /etc/NetworkManager/system-connections/enp1s0.nmconnection"

        elif echo $template | grep ubuntu -q ; then
        virt-sysprep -a $targetimg --hostname ${ip} --run-command "ssh-keygen -A" --root-password password:${root_passwd}  --ssh-inject root:file:/root/.ssh/id_rsa.pub --write "/etc/netplan/00-installer-config.yaml":"
network:
  ethernets:
    eth0:
      addresses:
      - ${ip}/24
      nameservers:
        addresses:
        - 192.168.0.133
        search: []
      routes:
      - to: default
        via: 192.168.0.1
  version: 2
"
        elif echo $template | grep centos -q ; then
        virt-sysprep -a $targetimg --root-password password:${root_passwd} --run-command "echo -e 'TYPE=Ethernet\nBOOTPROTO=static\nNAME=eth0\nDEVICE=eth0\nONBOOT=yes\nIPADDR=$ip\nNETMASK=255.255.255.0\nGATEWAY=192.168.0.1\nDNS1=114.114.114.114\n' > /etc/sysconfig/network-scripts/ifcfg-eth0"
        fi


        #virt-install --virt-type kvm --name $targetname --vcpus 4 --ram 8172 --disk $targetimg,format=qcow2 --network bridge=br0,model=virtio,alias.name=eth0 --network bridge=br1,model=virtio,alias.name=eth1 --graphics vnc,listen=0.0.0.0 --noautoconsole --osinfo=centos7 --import
        virt-install --virt-type kvm --name $targetname --vcpus 4 --ram 4000 --disk $targetimg,format=qcow2 --network bridge=br0,model=virtio,alias.name=eth0 --graphics vnc,listen=0.0.0.0 --noautoconsole --osinfo=centos7 --import
done
}



add_openstack() {
        add controller 192.168.0.{231..233}
        add network 192.168.0.{234..235}
        add compute 192.168.0.{236..237}
}
add_openstack_old() {
        add controller 192.168.0.99 
        add compute 192.168.0.199
        add middleware 192.168.0.123
}

add_ceph() {
        add rl-ceph 192.168.0.139 192.168.0.201 192.168.0.222
}


add_k8s() {
        # 3 master
        # 3 node
        #add k8s 192.168.0.{150..153}
        #add k8s 192.168.0.155
        #add k8s 192.168.0.157
#        add k8s 192.168.0.158
#        add k8s 192.168.0.159
        #add k8s 192.168.0.{160..161}
        add k8s 192.168.0.{162..163}
}


add_node() {
 add node 192.168.0.{133..138}
}

add_k8s_2() {
        # 3 master
        # 3 node
        add k8s 192.168.0.{160..165}
}

add_test() {
        #add k8s 192.168.0.233
        #add k8s 192.168.0.154
        add ldap 192.168.0.156
}
delete_node() {

        local name=$1
        shift
        local ips=$@
        local i=1
for ip in $ips; do
        local targetname=$name-$ip
        local targetimg=$workdir/$targetname.img
        virsh destroy $targetname
        virsh undefine $targetname  --remove-all-storage --delete-snapshots --managed-save
        rm -fr $targetimg
done
}
main() {
        local ACTION=$1
        shift 1
        local NAME=$1
        shift 1
        local REMOVE_IPS=$@

        #
workdir=/nvme_data/images
#template=$workdir/rockylinux-template-compress.img
#template=/data/isos/centos-7.9-2009-template.qcow2
template=/nvme_data/virtualhosts/ubuntu-22.04.2-compress.qcow2
#template=/nvme_data/virtualhosts/k8s-ubuntu-1.29.5.img
root_passwd=123

        case $ACTION in
                del)
                        delete_node $NAME $REMOVE_IPS;;
                *)
                        #add_test
                        #add_k8s
                        add_node
                        #add_openstack
                        #add_ceph
                        #add_k8s
                        ;;
        esac
}

main $@